package com.flame.parameter.controller;

import java.util.List;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.parameter.controller.vo.ParamsVo;
import com.flame.parameter.entity.Params;
import com.flame.parameter.service.ParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 *
 * @author wuxintong
 * @since 2022/10/26
 */
@RestController
public class ParamsController {

    @Autowired
    private ParamsService paramsService;

    @HasAuth({"params", "password-strategy"})
    @PostMapping("/params/page")
    public PageResult pageParams(@RequestBody ParamsVo paramsVo) {
        return PageResult.success(paramsService.pageParams(paramsVo));
    }

    @HasAuth("params:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/params/add")
    public Result addParams(@RequestBody @Validated Params params) {
        paramsService.addParams(params);
        return Result.success();
    }

    @HasAuth("params:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/params/update")
    public Result updateParams(@RequestBody @Validated Params params) {
        paramsService.updateParams(params);
        return Result.success();
    }

    @HasAuth({"params:edit", "password-strategy:edit"})
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/params/batch/update")
    public Result updateParams(@RequestBody @Validated List<Params> paramsList) {
        for (Params params : paramsList) {
            paramsService.updateParams(params);
        }
        return Result.success();
    }

    @HasAuth("params:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/params/delete")
    public Result deleteParams(@RequestBody @Validated Params params) {
        paramsService.deleteParams(params.getParamCode());
        return Result.success();
    }

    @HasAuth("params")
    @PostMapping("/params/group/query-all")
    public Result queryParamsGroup() {
        return Result.success(paramsService.queryParamsGroup());
    }
}
